(require 'init-package)

(when (maybe-require-package 'consult)
  ;; (with-eval-after-load 'consult
  (require 'init-keybind)
  ;; C-k bindings in `my-leader-key', define in init-keybind
  (keymap-set my-leader-key "M-x" 'consult-mode-command)
  (define-key my-leader-key "h" 'consult-history)
  (define-key my-leader-key "k" 'consult-kmacro)
  (define-key my-leader-key "M" 'consult-man)
  (define-key my-leader-key "I" 'consult-info)
  (define-key my-leader-key (kbd "C-p") 'consult-project-buffer)      ;; orig. project-switch-to-buffer
  ;; ([remap Info-search] . consult-info)
  ;; C-x bindings in `ctl-x-map'
  ;; ("C-x M-:" . consult-complex-command)     ;; orig. repeat-complex-command
  ;; ("C-x b" . consult-buffer)                ;; orig. switch-to-buffer
  ;; ("C-x 4 b" . consult-buffer-other-window) ;; orig. switch-to-buffer-other-window
  ;; ("C-x 5 b" . consult-buffer-other-frame)  ;; orig. switch-to-buffer-other-frame
  ;; ("C-x t b" . consult-buffer-other-tab)    ;; orig. switch-to-buffer-other-tab
  ;; ("C-x r b" . consult-bookmark)            ;; orig. bookmark-jump
  ;; Custom M-# bindings for fast register access
  (global-set-key (kbd "M-#") 'consult-register-load)
  (global-set-key (kbd "M-'") 'consult-register-store)          ;; orig. abbrev-prefix-mark (unrelated)
  (global-set-key (kbd "C-M-#") 'consult-register)
  ;; Other custom bindings
  (global-set-key (kbd "M-y") 'consult-yank-pop)                ;; orig. yank-pop
  ;; M-g bindings in `goto-map'
  ;; (global-set-key (kbd "M-g") 'goto-map)
  (define-key goto-map "e" 'consult-compile-error)
  (define-key goto-map "f" 'consult-flymake)               ;; Alternative: consult-flycheck
  (define-key goto-map "g" 'consult-goto-line)             ;; orig. goto-line
  (define-key goto-map (kbd "M-g") 'consult-goto-line)           ;; orig. goto-line
  (define-key goto-map "o" 'consult-outline)               ;; Alternative: consult-org-heading
  (define-key goto-map "m" 'consult-mark)
  (define-key goto-map "k" 'consult-global-mark)
  (define-key goto-map "i" 'consult-imenu)
  (define-key goto-map "I" 'consult-imenu-multi)
  ;; M-s bindings in `search-map'
  (define-key search-map "f" 'consult-find)                  ;; Alternative: consult-fd
  (define-key search-map "c" 'consult-locate)
  ;; (define-key search-map "g" 'consult-grep)
  (define-key search-map "G" 'consult-git-grep)
  (define-key search-map "g" 'consult-ripgrep)
  (define-key search-map "l" 'consult-line)
  (define-key search-map "L" 'consult-line-multi)
  (define-key search-map "k" 'consult-keep-lines)
  (define-key search-map "u" 'consult-focus-lines)
  ;; Isearch integration
  (define-key search-map "e" 'consult-isearch-history)
  ;;  isearch-mode-map
  (define-key isearch-mode-map (kbd "M-e") 'consult-isearch-history)   ;; orig. isearch-edit-string
  (define-key isearch-mode-map (kbd "M-s e") 'consult-isearch-history) ;; orig. isearch-edit-string
  (define-key isearch-mode-map (kbd "M-s l") 'consult-line) ;; needed by consult-line to detect isearch
  (define-key isearch-mode-map (kbd "M-s L") 'consult-line-multi) ;; needed by consult-line to detect isearch
  ;; Minibuffer history bindings in  minibuffer-local-map
  (define-key minibuffer-local-map (kbd "M-s") 'consult-history)  ;; orig. next-matching-history-element
  (define-key minibuffer-local-map (kbd "M-r") 'consult-history)  ;; orig. previous-matching-history-element

  ;; Enable automatic preview at point in the *Completions* buffer. This is
  ;; relevant when you use the default completion UI.
  ;; :hook (completion-list-mode . consult-preview-at-point-mode)

  ;; Tweak the register preview for `consult-register-load',
  ;; `consult-register-store' and the built-in commands.  This improves the
  ;; register formatting, adds thin separator lines, register sorting and hides
  ;; the window mode line.
  (advice-add #'register-preview :override #'consult-register-window)
  (setq register-preview-delay 0.5)

  ;; Use Consult to select xref locations with preview
  (setq xref-show-xrefs-function #'consult-xref
        xref-show-definitions-function #'consult-xref)


  ;; Optionally configure preview. The default value
  ;; is 'any, such that any key triggers the preview.
  ;; (setq consult-preview-key 'any)
  ;; (setq consult-preview-key "M-.")
  ;; (setq consult-preview-key '("S-<down>" "S-<up>"))
  ;; For some commands and buffer sources it is useful to configure the
  ;; :preview-key on a per-command basis using the `consult-customize' macro.
  ;; (consult-customize
  ;;  consult-theme :preview-key '(:debounce 0.2 any)
  ;;  consult-ripgrep consult-git-grep consult-grep consult-man
  ;;  consult-bookmark consult-recent-file consult-xref
  ;;  consult--source-bookmark consult--source-file-register
  ;;  consult--source-recent-file consult--source-project-recent-file
  ;;  ;; :preview-key "M-."
  ;;  :preview-key '(:debounce 0.4 any))

  ;; Optionally configure the narrowing key.
  ;; Both < and C-+ work reasonably well.
  (setq consult-narrow-key "<") ;; "C-+"

  ;; Optionally make narrowing help available in the minibuffer.
  ;; You may want to use `embark-prefix-help-command' or which-key instead.
  ;; (keymap-set consult-narrow-map (concat consult-narrow-key " ?") #'consult-narrow-help)
  )

(provide 'init-consult)
